package com.zmn.oms.admin.controller.conf.ordertag;

import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.conf.ordertag.ConfOrderTagBService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTag;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagQuery;
import com.zmn.oms.model.vo.conf.ordertag.ConfOrderTagVO;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：订单标签配置
 *
 * @author liuying
 * @date 2021/07/29 17:45
 */
@Controller
@RequestMapping("/conf/ordertag")
public class ConfOrderTagController extends OmsBaseController {

    @Resource
    private ConfOrderTagService confOrderTagService;
    @Resource
    private ConfOrderTagBService confOrderTagBService;

    @RequiresPermissions("/oms/conf/ordertag/list.action")
    @RequestMapping(value = "/list")
    public ModelAndView list(ModelAndView mv) {
        mv.setViewName("conf/ordertag/list");
        return mv;
    }

    /**
     * 查询列表 ajax
     */
    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(ConfOrderTagQuery query) {
        List<ConfOrderTagVO> confList = confOrderTagBService.listPageByQuery(query);
        LayuiTableDO<ConfOrderTagVO> data = new LayuiTableDO();
        data.setCount(query.getTotalCount());
        data.setData(confList);
        return ResultDTO.success(data);
    }

    /**
     * 查看/编辑
     *
     * @param mv
     * @param id
     * @return
     */
    @RequestMapping(value = "/addEdit")
    public ModelAndView detail(ModelAndView mv, Integer id) {
        mv.setViewName("conf/ordertag/detail");
        ConfOrderTag conf;
        if (Objects.nonNull(id)) {
            conf = confOrderTagService.findConfById(id);
        } else {
            conf = ConfOrderTag.builder().build();
        }
        mv.addObject("conf", conf);
        return mv;
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public ResultDTO save(@RequestBody ConfOrderTag conf) {
        try {
            if (Objects.nonNull(conf.getId())) {
                conf.setUpdater(super.getCurrentStaff().getRealName());
                confOrderTagBService.modifyConfById(conf);
                return ResultDTO.success();
            } else {
                conf.setCreater(super.getCurrentStaff().getRealName());
                Integer id = confOrderTagBService.addConf(conf);
                return ResultDTO.success(id);
            }
        } catch (OmsBaseException e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 改状态
     */
    @RequestMapping(value = "/modifyStatus")
    @ResponseBody
    public ResultDTO modifyStatus(ConfOrderTag conf) {
        logger.info("confCheckout[{}]", conf);
        conf.setUpdater(super.getCurrentStaff().getRealName());
        if(conf.getId() < 1000){
            return ResultDTO.fail("内置配置，不允许修改");
        }
        confOrderTagBService.modifyConfStatusById(conf);
        return ResultDTO.success();
    }

    /**
     * 改sort
     */
    @RequestMapping(value = "/modifySort")
    @ResponseBody
    public ResultDTO modifySort(ConfOrderTag conf) {
        logger.info("confCheckout[{}]", conf);
        conf.setUpdater(super.getCurrentStaff().getRealName());
        confOrderTagBService.modifyConfSortById(conf);
        return ResultDTO.success();
    }

}
